home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / utility-src / fileutils / src / rmdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-08  |  3.9 KB  |  169 lines  |  [TEXT/EMAC]

  1. /* rmdir -- remove directories
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Options:
  19.    -p, --parent        Remove any parent dirs that are explicitly mentioned
  20.             in an argument, if they become empty after the
  21.             argument file is removed.
  22.  
  23.    David MacKenzie <djm@ai.mit.edu>  */
  24.  
  25. #ifdef HAVE_CONFIG_H
  26. #if defined (CONFIG_BROKETS)
  27. /* We use <config.h> instead of "config.h" so that a compilation
  28.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  29.    (which it would do because it found this file in $srcdir).  */
  30. #include <config.h>
  31. #else
  32. #include "config.h"
  33. #endif
  34. #endif
  35.  
  36. #include "stdio.h"
  37. #include "getopt.h"
  38. #include "sys/types.h"
  39. #include "system.h"
  40. #include "version.h"
  41.  
  42. void error ();
  43. void strip_trailing_slashes ();
  44.  
  45. static void remove_parents ();
  46. static void usage ();
  47.  
  48. /* The name this program was run with. */
  49. char *program_name;
  50.  
  51. /* If nonzero, remove empty parent directories. */
  52. static int empty_paths;
  53.  
  54. /* If non-zero, display usage information and exit.  */
  55. static int show_help;
  56.  
  57. /* If non-zero, print the version on standard output and exit.  */
  58. static int show_version;
  59.  
  60. static struct option /* const */ longopts[] =
  61. {
  62.   {"path", no_argument, NULL, 1},
  63.   {"parents", no_argument, NULL, 1},
  64.   {"help", no_argument, NULL, 1},
  65.   {"version", no_argument, NULL, 1},
  66.   {"", 0, NULL, 0} // NULL changed to ""
  67. };
  68.  
  69. void
  70. main (argc, argv)
  71.      int argc;
  72.      char **argv;
  73. {
  74.   int errors = 0;
  75.   int optc;
  76.   
  77.   longopts[0].flag = &empty_paths;
  78.   longopts[1].flag = &empty_paths;
  79.   longopts[2].flag = &show_help;
  80.   longopts[3].flag = &show_version;
  81.  
  82.   program_name = argv[0];
  83.   empty_paths = 0;
  84.  
  85.   while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
  86.     {
  87.       switch (optc)
  88.     {
  89.     case 0:            /* Long option. */
  90.       break;
  91.     case 'p':
  92.       empty_paths = 1;
  93.       break;
  94.     default:
  95.       usage (1);
  96.     }
  97.     }
  98.  
  99.   if (show_version)
  100.     {
  101.       printf ("%s\n", version_string);
  102.       exit (0);
  103.     }
  104.  
  105.   if (show_help)
  106.     usage (0);
  107.  
  108.   if (optind == argc)
  109.     usage (1);
  110.  
  111.   for (; optind < argc; ++optind)
  112.     {
  113.       /* Stripping slashes is harmless for rmdir;
  114.      if the arg is not a directory, it will fail with ENOTDIR.  */
  115.       strip_trailing_slashes (argv[optind]);
  116.       if (rmdir (argv[optind]) != 0)
  117.     {
  118.       error (0, errno, "%s", argv[optind]);
  119.       errors = 1;
  120.     }
  121.       else if (empty_paths)
  122.     remove_parents (argv[optind]);
  123.     }
  124.  
  125.   exit (errors);
  126. }
  127.  
  128. /* Remove any empty parent directories of `path'.
  129.    Replaces '/' characters in `path' with NULs. */
  130.  
  131. static void
  132. remove_parents (path)
  133.      char *path;
  134. {
  135.   char *slash;
  136.  
  137.   do
  138.     {
  139.       slash = rindex (path, '/');
  140.       if (slash == NULL)
  141.     break;
  142.       /* Remove any characters after the slash, skipping any extra
  143.      slashes in a row. */
  144.       while (slash > path && *slash == '/')
  145.     --slash;
  146.       slash[1] = 0;
  147.     }
  148.   while (rmdir (path) == 0);
  149. }
  150.  
  151. static void
  152. usage (status)
  153.      int status;
  154. {
  155.   if (status != 0)
  156.     fprintf (stderr, "Try `%s --help' for more information.\n",
  157.          program_name);
  158.   else
  159.     {
  160.       printf ("Usage: %s [OPTION]... DIRECTORY...\n", program_name);
  161.       printf ("\
  162. \n\
  163.   -p, --parents   remove explicit parent directories if being emptied\n\
  164.       --help      display this help and exit\n\
  165.       --version   output version information and exit\n");
  166.     }
  167.   exit (status);
  168. }
  169.